package com.kang.scheduling;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

@Component
@EnableScheduling
@Slf4j
public class HotWord {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Scheduled(cron = "0 0 0 */1 * * ")   //凌晨12点跑
    public void deleteUpload() {
        try {
            Calendar calendar = Calendar.getInstance();
//        calendar.set(Calendar.YEAR,2022);
            //定时任务跑接口  设置为每天凌晨更新
            calendar.add(Calendar.DAY_OF_YEAR,1);
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.MILLISECOND,0);
            //晚上十二点与当前时间的秒差
            long timeOut = (calendar.getTimeInMillis()-System.currentTimeMillis()) / 1000;
            stringRedisTemplate.expire("hotWord",timeOut, TimeUnit.SECONDS);
        } catch (Exception ex) {
            log.error("定时获取热点信息错误",ex);
        }
    }
}
